<template>
    <div class="layout">
         <Form ref="formInline" :model="formInline" :rules="ruleInline" block :style="{'width':'220px','margin':'100px'}" v-show="regist">
        <FormItem prop="user">
            <Input type="text" v-model="formInline.user" placeholder="Username">
                <Icon type="ios-person-outline" slot="prepend"></Icon>
            </Input>
        </FormItem>
        <FormItem prop="password">
            <Input type="password" v-model="formInline.password" placeholder="Password">
                <Icon type="ios-locked-outline" slot="prepend"></Icon>
            </Input>
        </FormItem>
        <FormItem>
            <Button type="primary" @click="handleSubmit('formInline')">登陆</Button>
        </FormItem>
    </Form>
        <Layout :style="{minHeight: '100vh'}" v-show="login">
            <Sider collapsible :collapsed-width="78" v-model="isCollapsed">
                <Menu theme="dark" width="auto" :class="menuitemClasses">
                    <MenuItem name="1-1">
                    <router-link tag="div" to="user">
                        <Icon type="android-person"></Icon>
                        <span>用户管理</span>
                    </router-link> 
                    </MenuItem>
                    <MenuItem name="1-2">
                    <router-link tag="div" to="/cate">
                        <Icon type="search"></Icon>
                        <span>分类管理</span>
                    </router-link>
                    </MenuItem>
                    <MenuItem name="1-3">
                        <router-link tag="div" to="/news">
                          <Icon type="settings"></Icon>
                          <span @click="clickNewTree">新闻管理</span>
                          <Tree :data="treeMenu" @on-select-change="onSelectChangeTree" v-show="newsTree" ></Tree>
                        </router-link>
                    </MenuItem>
                    <MenuItem name="1-4">
                        <router-link tag="div" to="/album">
                          <Icon type="android-image"></Icon>
                          <span>相册管理</span>
                        </router-link>
                    </MenuItem>
                    <MenuItem name="1-5">
                        <router-link tag="div" to="/product">
                          <Icon type="settings"></Icon>
                          <span @click="clickProTree">产品管理</span>
                          <Tree :data="proMenu" @on-select-change="onSelectChangeTree" v-show="prosTree"></Tree>
                        </router-link>
                    </MenuItem>
                </Menu>
            </Sider>
            <Layout>
                <Header :style="{background: '#fff', boxShadow: '0 2px 3px 2px rgba(0,0,0,.1)'}"></Header>
                <Content :style="{padding: '0 16px 16px'}">
                    <Breadcrumb :style="{margin: '16px 0'}">
                        <BreadcrumbItem>Home</BreadcrumbItem>
                        <BreadcrumbItem>Components</BreadcrumbItem>
                        <BreadcrumbItem>Layout</BreadcrumbItem>
                    </Breadcrumb>
                    <Card>
                        <div style="height: 600px">
                            <router-view></router-view>
                        </div>
                    </Card>
                </Content>
            </Layout>
        </Layout>
    </div>
</template>
<script>
    import Bus from './Bus';
    export default {
        data () {
            return {
                newsTree:false,
                prosTree:false,
                isCollapsed: false,
                treeMenu:[],//新闻数据
                proMenu:[],//产品数据
                cateId:'',
                login:false,
                regist:true,
                formInline: {
                    user: '',
                    password: ''
                },
                ruleInline: {
                    user: [
                        { required: true, message: 'Please fill in the user name', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: 'Please fill in the password.', trigger: 'blur' },
                        { type: 'string', min: 6, message: 'The password length cannot be less than 6 bits', trigger: 'blur' }
                    ]
                }
            };
        },
        computed: {
            menuitemClasses: function () {
                return [
                    'menu-item',
                    this.isCollapsed ? 'collapsed-menu' : ''
                ]
            }
        },
        created(){
            fetch('http://localhost:3000/cate/list/1').then(res => {
                return res.json();
            }).then( data => {
                var len = data.length;
                for(let i=0;i<len;i++){
                    this.treeMenu.push(data[i]);
                }
            })
            fetch('http://localhost:3000/cate/list/2').then(res => {
                return res.json();
            }).then( data => {
                var len = data.length;
                for(let i=0;i<len;i++){
                    this.proMenu.push(data[i]);
                }
            })
        },
        methods:{
            onSelectChangeTree(data){
                Bus.$emit('newsData',data);
            },
            clickNewTree(){
                this.newsTree = !this.newsTree;
            },
            clickProTree(){
                this.prosTree = !this.prosTree;
            },
            handleSubmit(name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        this.$Message.success('登陆成功!');
                        this.login = true;
                        this.regist = false;
                    } else {
                        this.$Message.error('Fail!');
                    }
                })
            }
        },
        
    }
</script>

<style scoped>
    .layout-con{
        height: 100%;
        width: 100%;
    }
    .menu-item span{
        display: inline-block;
        overflow: hidden;
        width: 69px;
        text-overflow: ellipsis;
        white-space: nowrap;
        vertical-align: bottom;
        transition: width .2s ease .2s;
    }
    .menu-item i{
        transform: translateX(0px);
        transition: font-size .2s ease, transform .2s ease;
        vertical-align: middle;
        font-size: 16px;
    }
    .collapsed-menu span{
        width: 0px;
        transition: width .2s ease;
    }
    .collapsed-menu i{
        transform: translateX(5px);
        transition: font-size .2s ease .2s, transform .2s ease .2s;
        vertical-align: middle;
        font-size: 22px;
    }
</style>